package com.lb.trac.controller.dataloader;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;

import com.lb.trac.pojo.Utenti;
import com.lb.trac.service.SearchService;
import com.lb.trac.util.SQLCondition;

public class UsersDataLoader implements DataLoader {
	private SearchService searchService;
	private String[] columns;
	private String[] headers;
	private String storeId;
	
	private Map<String,String> propertiesMapper ;
	
	private Comparator<String> comparator = new Comparator<String>() {

		@Override
		public int compare(String s1,String s2) {
			Map<String,Integer> map = new HashMap<String, Integer>();
			for (int i = 0; i < columns.length; i++) {
				String col = columns[i];
				map.put(col, i);
			}
			
			return map.get(s1).compareTo(map.get(s2));
		}
		
	};
	

	
	@Override
	public Object load(Object... params) throws Exception {
		SQLCondition sqlCondition = null;
		if(params.length>0){
			sqlCondition=(SQLCondition) params[0];
		}
		HttpServletRequest request = (HttpServletRequest) params[1];
		Utenti[] users = getSearchService().find(new Utenti[0], sqlCondition, Utenti.class);
		JSONArray jsonFields = DataLoaderUtil.fieldsToJson(request,columns);
		JSONArray jsonData = DataLoaderUtil.dataToJson(request,comparator,columns,propertiesMapper,users);
		JSONArray jsonColumns = DataLoaderUtil.columnsToJson(request,headers,columns);
		
		return null;
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public String[] getColumns() {
		return columns;
	}

	public void setColumns(String[] columns) {
		this.columns = columns;
	}

	public Map<String, String> getPropertiesMapper() {
		return propertiesMapper;
	}

	public void setPropertiesMapper(Map<String, String> propertiesMapper) {
		this.propertiesMapper = propertiesMapper;
	}

	public String[] getHeaders() {
		return headers;
	}

	public void setHeaders(String[] headers) {
		this.headers = headers;
	}



}
